package com.mall.b2c.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.domain.member.CommentDomain;
import com.mall.b2c.service.CommentService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.b2c.util.ShopUtil;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;

/**
 * 评论
 * @author wangzx
 */
@Controller
@RequestMapping("/admin/b2c/comment")
public class CommentController  extends BaseController{
	@Resource CommentService commentService;
	//submit提交后，关闭当前的tab，并且刷新父页面
	protected static final String CLOSEANRELOAPARENT = "/admin/common/closeAndReloadParent"; 
	
	/**
	 * 跳转到列表页面
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		return "/admin/b2c/comment/list";
	}

	/**
	 * 获取列表信息
	 */
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Pager pager, HttpServletRequest request){
    	Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
    	pager = commentService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
    }
	
	/**
	 * 查看 
	 */
	@Validations(
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "ID非法!")
		}
	)
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String view(Long id, Model model){
		CommentDomain comment = commentService.find(id);
    	model.addAttribute("comment", comment);
		return "/admin/b2c/comment/input";
	}
	
    /**
	 * 删除评论
	 */
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "ids非法")	
		}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(Long[] ids, HttpServletResponse response){
		commentService.remove(ids, ShopUtil.getUser().getUsername());
    	return ResponseUtils.renderJSON(successMessage, response);
    }
}


